package com.lagou.Test;

import com.lagou.domain.Article;
import com.lagou.mapper.ArticleMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test1 {

    @Test
    public void testSelect() throws IOException {
        //System.out.println("hello, world");

        // 规范化   加载核心配置文件 ->   使用工厂模式获得其对象 ——>    获取sqlSession 的 会话对象
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //获取mapper 的代理对象
        ArticleMapper mapper = sqlSession.getMapper(ArticleMapper.class);

        //此处返回一个泛型为 Article 的list 集合
        List<Article> articles = mapper.findAll();

        //遍历打印
        for (Article article: articles){
            System.out.println(article);
            System.out.println(article.getComments());
        }

        //释放资源
        sqlSession.close();


    }

}
